//
//  ZMSearchProductCollectionView.swift
//  LoveBeen
//
//  Created by pg on 2017/8/9.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

class ZMSearchProductCollectionView: UICollectionView {

   
    let flowLayout = UICollectionViewFlowLayout()
    
    //数据源数组
    var productDataArray: [ZMProductsModel]?{
        didSet{
            
            reloadData()
        }
    }
    //闭包向外传递数据
    var collectionviweCellSelected:((String)->())?
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        
        self.backgroundColor = UIColor(white: 0.95, alpha: 1)
        delegate = self
        dataSource = self
        setupflowLayout()
      //  setupUI()
    }
        
    //MARK:- 初始化collectionview布局对象  flowlayout
    private func setupflowLayout(){
        
        flowLayout.itemSize = CGSize(width: (UIScreen.main.bounds.width - 5)*0.5, height: 280)
        flowLayout.minimumLineSpacing = 5
        flowLayout.minimumInteritemSpacing = 5
        //注册cell
        register(ZMProductCell.self, forCellWithReuseIdentifier: "cell")
        
        //注册组头view
        register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header")
    }
    
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
}

//collectionview 数据源和代理方法
extension ZMSearchProductCollectionView: UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       
        return self.productDataArray?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! ZMProductCell
        
        cell.productModel = self.productDataArray![indexPath.row]
        
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
       
        collectionviweCellSelected?("")
        
    }
    
    
    
    //MARK:- 设置collectionview组头视图
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        
        var headerview : UICollectionReusableView = UICollectionReusableView()
        
        if kind == UICollectionElementKindSectionHeader{
            
            headerview = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header", for: indexPath)
            
            
        }
        
        return headerview
        
    }
   
    
}

